package com.water.guider

import android.animation.ValueAnimator
import android.graphics.Point
import android.view.View
import android.view.animation.DecelerateInterpolator
import androidx.annotation.ColorInt
import androidx.annotation.LayoutRes
import com.water.guider.highlight.Highlight.rect
import com.water.guider.highlight.IHighlight
import com.water.guider.position.IPosition
import com.water.guider.position.Position.center

/**
 * 引导页.
 */
class Guide(builder: Builder) {

    /**
     * 引导页的x坐标
     */
    var x: Int

    /**
     *引导页的y坐标
     */
    var y: Int

    var pointView: View? = null
        set(value) {
            field = value
            val location = IntArray(2)
            field?.let {
                it.getLocationInWindow(location)
                x = location[0] + it.width / 2
                y = location[1] + it.height / 2
            }
        }

    var position: IPosition

    var highlight: IHighlight

    var targetView: View?
        private set

    var targetLayoutId: Int

    var view: View? = null

    var viewId: Int

    var onConvertListener: OnConvertListener?
    var onGuideListener: OnGuideListener?
    var animator: ValueAnimator?
    var backgroundColor: Int


    fun setTargetView(targetView: View) {
        this.targetView = targetView
    }

    /**
     * Builder class which makes it easier to create [Guide]
     */
    class Builder {
        var x = 0
        var y = 0
        var pointView: View? = null
        var position = center()
        var highlight: IHighlight = rect()
        var targetLayoutId = 0
        var targetView: View? = null
        var viewId = 0
        var view: View? = null
        var onGuideListener: OnGuideListener? = null
        var onConvertListener: OnConvertListener? = null
        var animator: ValueAnimator? = null
        var backgroundColor = -0x50000000

        /**
         * Sets the initial position of highlight
         *
         * @param y starting position of y where spotlight reveals
         * @param x starting position of x where spotlight reveals
         * @return This Builder
         */
        fun setPoint(x: Int, y: Int): Builder {
            this.x = x
            this.y = y
            return this
        }

        /**
         * Sets the initial position of highlight
         *
         * @param point starting position where spotlight reveals
         * @return This Builder
         */
        fun setPoint(point: Point): Builder {
            return setPoint(point.x, point.y)
        }

        /**
         * 设置想要添加高亮的View。
         * Sets the initial position of highlight
         * Make sure the view already has a fixed position
         *
         * @param pointView starting position where spotlight reveals
         * @return This Builder
         */
        fun setPoint(pointView: View): Builder {
            this.pointView = pointView
            return this
        }

        fun setOnGuideListener(listener: OnGuideListener): Builder {
            onGuideListener = listener
            return this
        }

        /**
         * 将设置的指引View或者layout通过ViewHolder的形式通过这个接口暴露。
         */
        fun setOnConvertListener(listener: OnConvertListener): Builder {
            onConvertListener = listener
            return this
        }

        /**
         * 设置高亮，有多种形状可选择，可实现IHighlight接口自定义位置。
         * @param highlight
         * @return
         */
        fun setHighlight(highlight: IHighlight): Builder {
            targetLayoutId = 0
            targetView = null
            this.highlight = highlight
            return this
        }

        fun setTarget(targetView: View): Builder {
            targetLayoutId = 0
            this.targetView = targetView
            return this
        }

        fun setTarget(@LayoutRes layoutId: Int): Builder {
            targetLayoutId = layoutId
            targetView = null
            return this
        }

        /**
         * 设置指引View的相对位置，有上下左右中等多个位置，可实现IPosition接口自定义位置。
         * @param position
         * @return
         */
        fun setPosition(position: IPosition): Builder {
            this.position = position
            return this
        }

        /**
         * 设置指引View，可以是View也可以是Layout。
         * @param viewId
         * @return
         */
        fun setView(@LayoutRes viewId: Int): Builder {
            view = null
            this.viewId = viewId
            return this
        }

        fun setView(descriptionView: View): Builder {
            viewId = 0
            view = descriptionView
            return this
        }

        fun setAnimator(animator: ValueAnimator): Builder {
            this.animator = animator
            return this
        }

        fun setBackground(@ColorInt backgroundColor: Int): Builder {
            this.backgroundColor = backgroundColor
            return this
        }

        fun build(): Guide {
            animator = ValueAnimator.ofInt(0, highlight.max).apply {
                interpolator = DecelerateInterpolator(2f)
                duration = 1500
            }
            return Guide(this)
        }
    }

    init {
        x = builder.x
        y = builder.y
        pointView = builder.pointView
        position = builder.position
        highlight = builder.highlight
        targetView = builder.targetView
        targetLayoutId = builder.targetLayoutId
        view = builder.view
        viewId = builder.viewId
        onGuideListener = builder.onGuideListener
        onConvertListener = builder.onConvertListener
        animator = builder.animator
        backgroundColor = builder.backgroundColor
    }
}